# -*- coding:utf-8 -*-

import itertools, datetime, time

# 获取当前
def get_now_date():
    now = int(time.mktime(datetime.datetime.now().timetuple())) + 28800
    arr = datetime.datetime.utcfromtimestamp(now)
    btime = arr.strftime("%Y-%m-%d %H:%M:%S")
    return btime

# 返回指定redis列表名
def redis_key_name(baId, key_type=None):
    if key_type is None:
        return baId
    elif key_type is "ss":  # 待执行的有序集合（bell任务）
        return "SS" + baId
    elif key_type is "rl":  # 已执行的任务列表（bell任务）
        return "RL" + baId
    elif key_type is "be":  # bell 检测
        return "BE" + baId
    elif key_type is "ml":  # 总任务集散仓库
        return "material"


# 将数组转换为sql文中可以使用的字符串
def arr_to_sqlStr(str_arr):
    newList = list(set(str_arr))
    return "\'" + ("\',\'".join(newList)) + "\'"


# 将sql返回的元组转换为数组
def tuple_to_sqlArr(sql_tuple):
    arr = []
    for each in sql_tuple:
        arr.append(str(each[0]))
    return arr


def build_insert_sql(inHead=None, inGroove=None, data=None):
    """
    创建insert的sql文
    :param inHead: sql文头部 
    :param inGroove: 占位符
    :param data: 数据
    :return: 
    """
    if not inHead or not data or not inGroove:
        return

    inBody, length, inNail, i = "", len(data), [], 0
    for each in data:
        del inNail[:]
        if type(each) == str:
            inNail.append(each)
        else:
            for j in xrange(len(each)):
                inNail.append(each[j])
        args = tuple(inNail)

        if i == length - 1:
            inBody = inBody + (inGroove % args)
        else:
            inBody = inBody + (inGroove % args) + ","
        i += 1

    finalSql = inHead + inBody
    return finalSql


# hmac加密，返回加密后的值
def get_hmac(key, data):
    import hmac
    hmacKey = hmac.new(key)
    hmacKey.update(data)
    return hmacKey.hexdigest()

# 添加包头
def length_pack(data):
    length = str(len(data))
    return length + data


# 编辑开关灯命令
def build_switch_command(*args):
    baId, need, targetId, mode = args[:]
    pointTask_groove = '%s,%s,%s'  # 基站id,命令,目标id
    command = None
    if mode == "lc":
        command = 'lnon' if need == 1 else 'lnof'
    elif mode == "se":
        command = 'seon' if need == 1 else 'seof'
    else:
        return

    taskStr = pointTask_groove % (baId, command, targetId)

    return taskStr